using System;
using System.Collections.Generic;
using UnityEngine;

namespace EventBehaviorTree
{
    [Serializable]
    public class Container : EventNode
    {
        protected List<EventNode> _subNodes = new List<EventNode>();
        protected int _stepCounter = 0;
        protected bool _isHoldSubNode = false;
        public void SetKey(string key) { _key = key; }
        /// <summary>
        /// if hold subNode, start subNodes; else send success to compositeNode
        /// </summary>
        protected void SubNodeCheck()
        {
            if (_isHoldSubNode) NodeEvent(NodeMessage.Start);
            else NodeEvent(NodeMessage.Success);
        }
        /// <summary>
        /// add node to the list and set the key for subNode
        /// </summary>
        /// <param name="subNode"></param>
        public void AddSubNodes(EventNode subNode)
        {
            _subNodes.Add(subNode);
            subNode._key = _key + "." + _stepCounter;
            _stepCounter++;
            subNode.AddEvent(NodeEvent);
            _isHoldSubNode = true;
        }
        public override void Initialize(MonoBehaviour behaviorTree)
        {
            base.Initialize(behaviorTree);
            _stepCounter = 0;
            foreach (EventNode item in _subNodes) { item.Initialize(_monoBehavior); }
        }
        public override void Reset()
        {
            _stepCounter = 0;
            foreach (EventNode subNode in _subNodes) { subNode.Reset(); }
        }
    }
}